#include "gfxTowerBeam.h"
#include <math.h>
#include <QDebug>

#define SQUARE_SIZE 20

GFXTowerBeam::GFXTowerBeam(ClientEntity *entity, QGraphicsItem *parent)
   : GFXItem(entity, parent)
{
   mAimDirection = 0;
   //qDebug() << "TowerBeamItem::TowerBeamItem";
   setFlag(QGraphicsItem::ItemIsSelectable);
}


GFXTowerBeam::GFXTowerBeam(const EntityBaseStats &stats, QGraphicsItem *parent)
   : GFXItem(stats, parent)
{
   setFlag(QGraphicsItem::ItemIsSelectable);
}


GFXTowerBeam::~GFXTowerBeam()
{
}


QRectF GFXTowerBeam::boundingRect() const
{
   return QRectF(-SQUARE_SIZE/2, -SQUARE_SIZE/2, SQUARE_SIZE, SQUARE_SIZE);
}


void GFXTowerBeam::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
   if (mUseEntity && mEntity)
   {
      doPaint(mEntity->base.color,
              mEntity->base.size,
              mShowRange,
              mBlendRange,
              *painter,
              mPulsate ? mPulsateScale : 1.0);
   }
   else
   {
      doPaint(mStats.color,
              mStats.size,
              mShowRange,
              mBlendRange,
              *painter,
              mPulsate ? mPulsateScale : 1.0);
   }
}


void GFXTowerBeam::tick()
{
   GFXItem::tick();

   if (mEntity)
   {
      setPos(mEntity->pos.toPointF());
      setRotation(mEntity->rotation);
   }
}


void GFXTowerBeam::doPaint(const QColor &color,
                            const qint32 size,
                            const bool showRange,
                            const bool blendRange,
                            QPainter& painter,
                            const qreal scale)
{
   painter.setPen(QPen(QColor(color),2));

   if (showRange)
   {
      QColor c(color);
      c.setAlpha(64);
      if (blendRange)
      {
         QBrush brush(c, Qt::SolidPattern);
         painter.setBrush(brush);
      }
      int range = (20+size*10);
      painter.drawEllipse(QPoint(0,0), range, range);
   }

   painter.scale(scale, scale);

   QPainterPath path;
   path.moveTo(0,0);
   path.lineTo(0.0, -8.0);
   path.lineTo(-8.0, 0.0);
   path.lineTo(0.0, 8.0);
   path.closeSubpath();
   painter.drawPath(path);

   painter.drawRect(-SQUARE_SIZE/2, -SQUARE_SIZE/2, SQUARE_SIZE, SQUARE_SIZE);
}


